package com.whl.sm.controller.inhabitant;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.vo.OrgUserAssetsOwnerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-14
 */
@RestController
@RequestMapping("/assets-owner")
public class AssetsOwnerController extends BaseController {

    @Autowired
    private AssetsOwnerService assetsOwnerService;

    @Autowired
    private OrgUserInfoService orgUserInfoService;

    @Autowired
    private ComUnitsService comUnitsService;

    @RequestMapping("/list")
    public Result getOwnerList(HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        String likeName = request.getParameter("likeName");
        if (!likeName.equals("undefined") && !likeName.equals("")){
            List<OrgUserInfo> userInfo = orgUserInfoService.list(new QueryWrapper<OrgUserInfo>().like("user_name",likeName));
            if (userInfo.size()>0){
                queryWrapper.in("user_id",userInfo.stream().map(OrgUserInfo::getUserId).collect(Collectors.toList()));
            }
        }
        setHttpRequestQueryWrapper(queryWrapper,"assets_id","owner_type");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> ownerPage = assetsOwnerService.page(getPage(),queryWrapper);
        List<Object> result = ownerPage.getRecords().stream().map(obj->{
                AssetsOwner assetsOwner = (AssetsOwner) obj;
                OrgUserInfo userInfo = orgUserInfoService.getOne(
                        new QueryWrapper<OrgUserInfo>().eq("user_id",assetsOwner.getUserId()));
                ComUnits units = comUnitsService.getById(assetsOwner.getAssetsId());
                return new OrgUserAssetsOwnerVO(userInfo,assetsOwner,units);
            }).collect(Collectors.toList());
        ownerPage.setRecords(result);
        return Result.ok(ownerPage);
    }


    @RequestMapping("/update")
    public Result updateUser(@RequestBody AssetsOwner assetsOwner){
        return assetsOwnerService.saveBind(assetsOwner);
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            assetsOwnerService.update(new UpdateWrapper<AssetsOwner>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

    @RequestMapping("/getMember/{assetsId}")
    public Result getMember(@PathVariable("assetsId") String asstesId){
        List<AssetsOwner> member = assetsOwnerService.list(new QueryWrapper<AssetsOwner>()
                .eq("assets_id",asstesId)
                .eq("owner_type",0)
        );
        List<OrgUserAssetsOwnerVO> result = member.stream().map(obj->{
            AssetsOwner assetsOwner = (AssetsOwner) obj;
            OrgUserInfo userInfo = orgUserInfoService.getOne(
                    new QueryWrapper<OrgUserInfo>().eq("user_id",assetsOwner.getUserId()));
            return new OrgUserAssetsOwnerVO(userInfo,assetsOwner,null);
        }).collect(Collectors.toList());
        return Result.ok(result);
    }

}
